<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;

class SystemController extends CommonController {
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        return view('admin.system.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        return view('admin.system.create', ['sortList' => $this->getCreate()]);
    }

    public function insert (Request $request){
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        return $this->insert($request);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id) {
        return view('admin.system.index', $this->lists());
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id) {
        $data = $this->getEdit($id);
        return view('admin.system.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id) {
        if (!$request->isMethod('PATCH')) {
            return null;
        }

        $data = $request->except(['_token', '_method']);
        $file = $data['file'];
        unset($data['file']);

        if($file != 'site.php'){
            return null;
        }

        if($data['title'] == ''){ $data['title'] = '来文设计'; }

        $arrurl = explode('/', $request->server('PHP_SELF'));
        $serve_name = $request->server('SERVER_NAME');
        if($arrurl[2] == 'admin'){
            $rooturl = 'http://' . $serve_name . '/';
        } else {
            $rooturl = 'http://' . $serve_name . $arrurl[2] . '/';
        }

        if($rooturl                 == ''){ $data['rooturl'] = $rooturl; }
        if($data['domain']          == ''){ $data['domain'] = 'http://www.jing-ui.com'; }
        if($data['title']           == ''){ $data['title'] = '来文设计'; }
        if($data['keywords']        == ''){ $data['keywords'] = 'JingJingui设计,网页设计,网站设计'; }
        if($data['description']     == ''){ $data['description'] = '设计师静静个人网站，分享设计，分享人生，提供优质的网页设计、UI设计、APP用户体验改善、UE设计等各项设计服务！'; }
        if($data['blogTitle']       == ''){ $data['blogTitle'] = '来文设计'; }
        if($data['blogKeywords']    == ''){ $data['blogKeywords'] = 'JingJingui设计,网页设计,网站设计'; }
        if($data['blogDescription'] == ''){ $data['blogDescription'] = '设计师静静个人网站，分享设计，分享人生，提供优质的网页设计、UI设计、APP用户体验改善、UE设计等各项设计服务！'; }
        if($data['contact']         == ''){ $data['contact'] = 'JingJingUi'; }
        if($data['company']         == ''){ $data['company'] = 'JingJingUI'; }
        if($data['email']           == ''){ $data['email'] = 'joucik@qq.com'; }
        if($data['phone']           == ''){ $data['phone'] = '123456'; }
        if($data['address']         == ''){ $data['address'] = '中国 • 上海'; }
        if($data['icp']             == ''){ $data['icp'] = ''; };

        $content = "<?php\r\n // jingjingui 网站配置文件 \r\n return array(\r\n";
        foreach($data as $key => $value){
            $content .= "\t'$key' => '$value', \r\n";
            config($key, $value);
        }

        $content .= "); \r\n ?>";

        $sitePath = config_path() . DIRECTORY_SEPARATOR . $file;
        $r = @chmod($sitePath, 0777);
        $hand = file_put_contents($sitePath, $content);

        if(!$hand){
            return redirect('/admin/system')->withErrors('保存出错');
        } else {
            Artisan::call('config:cache');
            return back()->withErrors('保存成功');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
    }

    public function tplUpdate (Request $request) {
        $type = $request->type;
        config(['view.tpl' => $type]);
        Artisan::call('config:cache');
    }
}
